package com.rttmall.shopbackend.app.gambling.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.fileupload.FileItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.alibaba.fastjson.JSON;
import com.rttmall.shopbackend.app.gambling.pojo.VirtualData;
import com.rttmall.shopbackend.app.gambling.pojo.VirtualDataCustom;
import com.rttmall.shopbackend.app.gambling.service.VritualDataService;
import com.rttmall.shopbackend.enums.VirtualStatus;
import com.rttmall.shopbackend.exception.ServiceException;
import com.rttmall.shopbackend.pojo.Pagination;
import com.rttmall.shopbackend.sys.pojo.Menu;
import com.rttmall.shopbackend.utils.Constants;

@Controller
@RequestMapping("virtualData")
public class VirtualDataController {
	@Autowired
	private VritualDataService vritualDataService;
	
	@RequestMapping(method = RequestMethod.GET)
	public String findVirtualData() {
		return "app/gambling/VirtualList";
	}
    @RequestMapping(value = "addvirtualDataPage")
    public String addvirtualDataPage() {
        return "app/gambling/VirtualAddEdit";
    }
    @RequestMapping(value = "importVirtualDataPage")
    public String importVirtualDataPage() {
        return "app/gambling/importVirtualData";
    }
	@RequestMapping(value = "/queryVirtualData")
	public @ResponseBody Map queryVirtualData(
			@RequestParam(defaultValue = "") String json, Pagination grid) {
		VirtualDataCustom virtualDataCustom = JSON.parseObject(json,
				VirtualDataCustom.class);
		if (null == virtualDataCustom) {
			virtualDataCustom = new VirtualDataCustom();
		}
		grid.setStartIndex(grid.getPageIndex() * grid.getPageSize());
		virtualDataCustom.setPagination(grid);
		Map map = vritualDataService.queryVirtualData(virtualDataCustom);
		return map;
	}

    @RequestMapping("/insertVirtualDataSubmit")
    public
    @ResponseBody
    void insertVirtualDataSubmit(VirtualData virtualData) throws Exception {
    	virtualData.setStatus(VirtualStatus.NOT_GRANT.ordinal());
    	vritualDataService.insertVirtualDataSubmit(virtualData);
    }
    @RequestMapping(value = "/editQueryVirtualData")
    public
    @ResponseBody
    VirtualDataCustom editQueryVirtualData(Integer id) {
        return vritualDataService.editQueryVirtualData(id);
    }
    @RequestMapping(value = "/updateVirtualDataSubmit")
    public
    @ResponseBody
    void updateVirtualDataSubmit(VirtualData virtualData) throws Exception {
    	vritualDataService.updateVirtualDataSubmit(virtualData);
    }
    
    @RequestMapping(value = "/deleteVirtualData")
    public
    @ResponseBody
    void deleteVirtualData(Integer id) throws Exception {
    	vritualDataService.deleteVirtualData(id);
    }
    @RequestMapping(value="importVirtual")
    public @ResponseBody int importVirtual(HttpServletRequest request, HttpServletResponse response) throws Exception{
    	int successCount = 0;
         // 处理上传文件
         MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
         CommonsMultipartFile file = (CommonsMultipartFile) multipartRequest.getFile("updatefile");
         if (file != null) {
             FileItem fileItem = file.getFileItem();
                 int dot = fileItem.getName().lastIndexOf('.'); 
                 String prefix = fileItem.getName().substring(dot+1);
                 if(prefix==null || !("xls".equals(prefix.toLowerCase()) || "xlsx".equals(prefix.toLowerCase()))){
                	 throw new ServiceException(Constants.FILE_FORMAT_ERROR);
                 }else{
                	 successCount = vritualDataService.batchImportVirtualData(fileItem.getName(),file);
                 }
         }
         return successCount;
    }
}
